package models

import (
	"app-box/api_error"
	"time"
)

type LoginPlatform string

const (
	PlatformIOS     LoginPlatform = "i"
	PlatformAndroid LoginPlatform = "a"
	PlatformWeb     LoginPlatform = "w"
)

type UserType int

const (
	UserTypeUser    UserType = 1      // 用户Token
	UserTypeAdmin   UserType = 1 << 1 // 管理员Token
	UserTypeUserApi UserType = 1 << 2 // 用户Api Token

	UserTypeAll UserType = UserTypeUser | UserTypeAdmin | UserTypeUserApi // 所有类型
)

func (t UserType) HasType(usertype UserType) bool {
	if usertype == 0 {
		return false
	}
	return usertype == usertype&t
}

type TokenType int

const (
	TokenTypeRefresh TokenType = iota
	TokenTypeLogin
)

type Token struct {
	ID           IDType        `json:"i"`  // 用户ID
	ExpirationAt int64         `json:"ex"` // Token过期时间,时间戳
	Platform     LoginPlatform `json:"p"`  // 登录平台
	UserType     UserType      `json:"t1"` // 用户类型
	TokenType    TokenType     `json:"t2"` // Token类型
}

func (t Token) Valid() error {
	if t.ExpirationAt < time.Now().Unix() {
		return api_error.TokenOutOfDate()
	}
	return nil
}

// API中返回给用户Token的模型
type TokenResult struct {
	Token        string
	RefreshToken string
}
